from django.shortcuts import render
from django.http import HttpResponse , JsonResponse

from django.contrib.auth.models import User,Permission,Group
from django.contrib.auth import authenticate, login,logout


# Create your views here.

# 管理员用户添加
def useradd(request):
	if request.method == 'GET':
		# 获取所有的组
		glist = Group.objects.all()
		context = {'glist':glist}
		# 显示添加页面
		return render(request,'auth/user/add.html',context)
		# 数据类型的操作 框架
	elif request.method == 'POST':
		# 执行管理员添加
		# 判断是否创建超级管理员
		if request.POST['is_superuser'] == '1':
			ob = User.objects.create_superuser(request.POST['username'],request.POST['email'],request.POST['password'])
		else:
			ob = User.objects.create_user(request.POST['username'],request.POST['email'],request.POST['password'])
		# 进行添加
		ob.save()

		# 判断是否需要为用户分配组
		gs = request.POST.getlist('gs',None)
		if gs:
			# 给当前用户分组
			ob.groups.set(gs)
			ob.save()
		return HttpResponse('<script>location.href="/admin/auth/user/list"</script>')

# 管理员用户列表
def userlist(request):


	# 搜索
	# 搜索类型
	types = request.GET.get('types',None)
	# 搜索关键字
	keywords = request.GET.get('keywords','')
	if types:
		if types == "all":
			data = User.objects.filter(name__contains=keywords)
		elif types == "id":
			data = User.objects.filter(id__contains=keywords)
		elif types == "username":
			data = User.objects.filter(username__contains=keywords)
		elif types == "email":
			data = User.objects.filter(email__contains=keywords)
		elif types == "addtime":
			data = User.objects.filter(date_joined__contains=keywords)
		elif types == "grade":
			listgrade = {"超级管理员":1,"普通管理员":0}

			data = User.objects.filter(is_superuser__contains=listgrade.get(keywords,'a2#f'))
		# 所属组

	else:
		# 获取所有的管理员
		data = User.objects.all()



	

	# 数据分页
	from django.core.paginator import Paginator
	# 实例化分页类 参数1-参数的数据，参数2-每页显示的数据
	paginator = Paginator(data,5)
	# 当前页码
	p = int(request.GET.get('p',1))
	# 根据当前页码获取当前页码应该显示的数据
	tlist = paginator.page(p)


	context = {"ulist":tlist}
	
	return render(request,'auth/user/list.html',context)
	# return HttpResponse('aa2')

# 删除管理员
def userdel(request,uid):
	ob = User.objects.get(id=uid)
	ob.delete()
	return HttpResponse('<script>location.href="/admin/auth/user/list"</script>')


# 管理组列表
def grouplist(request):
	# 获取所有的组
	data = Group.objects.all()

	# 搜索
	# 搜索类型
	types = request.GET.get('types',None)
	# 搜索关键字
	keywords = request.GET.get('keywords')
	if types:
		if types == "username":
			data = Group.objects.filter(name__contains=keywords)
	else:
		data = Group.objects.all()


	# 数据分页
	from django.core.paginator import Paginator
	# 实例化分页类 参数1-参数的数据，参数2-每页显示的数据
	paginator = Paginator(data,5)
	# 当前页码
	p = int(request.GET.get('p',1))
	# 根据当前页码获取当前页码应该显示的数据
	tlist = paginator.page(p)


	context = {'glist':tlist}
	return render(request,'auth/group/list.html',context)

# 管理组添加
def groupadd(request):
	if request.method == "GET":
		# perms = Permission.objects.all()
		# 读取所有权限信息,并排除因Can开头的系统默认生成的权限
		perms = Permission.objects.exclude(name__istartswith='Can')

		context = {'perms':perms}
		return render(request,'auth/group/add.html',context)
	elif request.method == "POST":
		# 创建组
		g = Group(name=request.POST['name'])
		g.save()
		# 获取选择的所有权限
		prms = request.POST.get('prms',None)
		# 判断是否需要给组添加权限
		if prms:
			# 给组分配权限
			g.permissions.set(prms)
			g.save()
		return HttpResponse('<script>location.href="/admin/auth/group/list"</script>')

def groupedit(request,gid):
	# 获取当前组的信息
	ginfo = Group.objects.get(id=gid)

	if request.method == 'GET':
		# 读取所有权限信息，并排除已经的权限
		perms = Permission.objects.exclude(group=ginfo).exclude(name__istartswith='Can')
		context = {'ginfo':ginfo,'perms':perms}
		return render(request,'auth/group/edit.html',context)
	elif request.method == 'POST':
		# 修改组名
		ginfo.name = request.POST['name']
		# 判断是否有权限
		prms = request.POST.getlist('prms',None)

		# 全部删除，再添加
		ginfo.permissions.clear()

		if prms:
			# 添加权限
			ginfo.permissions.set(prms)
		ginfo.save()

		return HttpResponse('<script>location.href="/admin/auth/group/list"</script>')


#登陆界面
def mylogin(request):
	# 如果访问当前页面的请求方式为GET，则返回一个登陆界面
	if request.method == "GET":
		return render(request,'admin/login.html')
	elif request.method == "POST":
		# 执行登录
		# 判断验证码是否正确
		if request.POST['vcode'].lower() != request.session['verifycode'].lower():
			return HttpResponse('<script>alert("验证码错误");location.href="/admin/login/";</script>')
		# 使用django提供的后台用户验证方法
		username = request.POST['username']
		password = request.POST['password']
		user = authenticate(request,username=username,password=password)
		if user:
			# 登录
			login(request,user)
			return HttpResponse('<script>location.href="/admin/";</script>')

		return HttpResponse('<script>alert("用户名或密码不存在");location.href="/admin/login/";</script>')

# 退出登录
def mylogout(request):
	# 读取前台登陆用户的session
	tmp = request.session['VipUser']

	# 退出登录
	logout(request)

	# 将前台用户的信息放入session
	request.session['VipUser'] = tmp 
	return HttpResponse('<script>alert("退出登录成功");location.href="/admin/login/";</script>')